package me.zhengjie.wcs;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.pojo.sqlServer.PPDeviceStatus;
import me.zhengjie.pojo.sqlServer.PPTaskResult;
import me.zhengjie.repository.PPDeviceStatusRepository;
import me.zhengjie.repository.PPTaskResultRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class PPWcsStatus {

    private final PPDeviceStatusRepository ppDeviceStatusRepository;
    private final PPTaskResultRepository ppTaskResultRepository;


    private static final Logger ppLog = LoggerFactory.getLogger("pp");

    /**
     * 判断当前码垛机时候可以接收入库任务
     * @return true可以发送，false不能发送
     */
    public boolean allowSendTask(){
        try {
            List<PPDeviceStatus> deviceStatusList = ppDeviceStatusRepository.findNewDeviceStatus();
            PPDeviceStatus deviceStatus = deviceStatusList.get(0);
            Integer status = deviceStatus.getStatus();
            return status == 1;
        } catch (Exception e) {
            ppLog.error("没有查询到码垛机状态信息");
            return false;
        }
//        return status == 0 || status == 2;
    }

    /**
     * 判断当前码垛机时候可以接收出库任务
     * @return true可以发送，false不能发送
     */
    public boolean allowSendOutBoundMission(String code){
        Integer status = ppDeviceStatusRepository.getStatusByCode(code);
        PPTaskResult taskResult = ppTaskResultRepository.findOutBoundTask();
        // 设备状态=1是正常，并且没有执行中的出库任务
        return status ==1 && !ObjectUtil.isEmpty(taskResult);
    }


    /**
     * 判断当前码垛机时候可以接收移库任务
     * @return true可以发送，false不能发送
     */
    public boolean allowSendMoveMission(String code){
        Integer status = ppDeviceStatusRepository.getStatusByCode(code);
        PPTaskResult taskResult = ppTaskResultRepository.findMoveTask();
        // 设备状态=1是正常，并且没有执行中的出库任务
        return status ==1 && !ObjectUtil.isEmpty(taskResult);
    }

    /**
     * 判断码垛机取货完成与否
     * @param code 码垛机编码
     * @return true表示取货完成，false表示取货没有完成
     */
    public boolean canStartPlc(String code){
        return true;
    }
}
